<?php
/**
*	监控摄影图像
*
*	@param
*	@return
*	@date	2012-9-21 21:57:00
*	@author lin.x
*
*/
class PhotographicModel extends Model{
	public function getItems(){
		$base = C('image_source_dir');
		$logModel = D('Log');

		if(empty($base)){
			$logModel->log("图片存放目录未设置!");
			return false;
		}

		if(!is_array($base)){
			$base = array($base);
		}

		foreach ($base as $key => $val) {
			if(!is_dir($val)){
				$logModel->log(sprintf("图片存放目录 %s 不存在", $val));
				continue;
			}
			$pattern = $val . '/*.{jpg,bmp,png,jpeg,gif}';
			$items = glob($pattern, GLOB_BRACE);
			$allitems[$key] = $items;
		}
		return $allitems;
	}

	public function addItems($items){
		import('ORG.Util.Image');
		$logModel = D('Log');

		$size = C('image_thumb_size');

		foreach ($items as $key => $_items) {
			foreach ($_items as $val) {
				if(!is_file($val)){
					continue;
				}


				$row['hash'] = md5($val);
				$row['dateline'] = $this->getFilectime($val);

				foreach ($size as $s) {
					$dir = ROOT . '/images/' . date('Y/m/d/', $row['dateline']);
					if(!is_dir($dir)){
						mkDirs($dir);
					}
					$thumbname = $dir . $row['hash'] . '_' . join('_', $s) . '.jpg';
					Image::thumb($val, $thumbname, 'jpg', $s[0], $s[1]);
				}

				//转存原图
				$thumbname = $dir . $row['hash'] . '.jpg';
				$info  = Image::getImageInfo($val);
				Image::thumb($val, $thumbname, 'jpg', $info['width'], $info['height']);
				$res = unlink($val);
				if(!$res){
					$logModel->log(sprintf("删除文件 %s 失败!", $val));
				}

				$row['cid'] = $key;
				$row['path'] = str_replace(ROOT, '', $thumbname);
				$rows[] = $row;
			}
		}

		$res = $this->addAll($rows, array(), true);
//		dp($res, $rows, $this->getLastSql(), $this->getDbError());

		$logModel->log(sprintf("共计导入图片 %d 张", $res));

		return $res;
	}
	/**
	*	获取图片真实生成时间
	*
	*	@param	$val 图片文件名
	*	@return
	*	@date	2012-9-21 21:02:37
	*	@author lin.x
	*
	*/
	public function getFilectime($val){
		if(preg_match('/\d{14}/', basename($val), $match)){
			$dateline = $match[0];
			return strtotime($dateline);
		}
	}
	/**
	*	获取图片列表
	*
	*	@param $cid 图片分组/摄像头
	*	@param $dateline 起始时间
	*	@param $limit 图片数量
	*	@return
	*	@date
	*	@author lin.x
	*
	*/
	public function getList($cid, $dateline, $limit=10){
		$data = $this->field('dateline, path')->where(array(
			'cid'		=>	$cid,
			'dateline'	=>	array('GT', $dateline)
			))->limit($limit)->order('dateline DESC')->select();
		foreach ($data as $val) {
			$imgs[$val['dateline']] = $val['path'];
		}
		return $imgs;
	}
	/**
	*	获取不同尺寸图片
	*
	*	@param
	*	@return
	*	@date
	*	@author lin.x
	*
	*/
	public function getSize($imgs, $size){
		$prefix = dirname(__APP__);
		foreach ($imgs as &$val) {
			$val = $prefix . str_replace('.jpg', '', $val) . '_' . join('_', $size) . '.jpg';
		}
		return $imgs;
	}
	/**
	*	获取最新的各摄像头图片
	*
	*	@param
	*	@return
	*	@date
	*	@author lin.x
	*
	*/
	public function getLastestPics(){
		$ds = C('IMAGE_SOURCE_DIR');
		$sql = "SELECT * FROM __TABLE__ WHERE cid = '%s' ORDER BY DATELINE DESC LIMIT 1";
		foreach ($ds as $key => $val) {
			$sqls[] = sprintf($sql, $key);
		}
		$sql = sprintf("(%s)", join(") UNION (", $sqls));

		$res = $this->query($sql, false);

		$size = C('IMAGE_THUMB_SIZE');
		$prefix = dirname(__APP__);

		foreach ($res as &$val) {
			foreach ($size as $k=>$s) {
				$val['img_' . $k] = $prefix . str_replace('.jpg', '', $val['path']) . '_' . join('_', $s) . '.jpg';
			}
			$val['alt'] = sprintf("拍摄时间 %s", date('Y-m-d H:i:s', $val['dateline']));
		}
		return $res;
	}
}